//
//  UIButton+ZXHelper.m
//  MusiceFate
//
//  Created by simon on 13/3/21.
//  Copyright © 2013年 yinyuetai.com. All rights reserved.
//

#import "UIButton+ZXHelper.h"

@implementation UIButton (ZXHelper)


- (void)zx_setImagePositionWithType:(CAIButtonImagePlacement)type spacing:(CGFloat)spacing
{
    ///self.frame:CGRect(0,0,0,0),self.imageView.frame:(5.5 0; 41 0)
//    CGFloat imageWidth  = self.imageView.frame.size.width;
//    CGFloat imageHeight = self.imageView.frame.size.height;
    CGFloat imageViewWidth  = self.imageView.intrinsicContentSize.width;
    CGFloat imageViewHeight = self.imageView.intrinsicContentSize.height;
    CGFloat lableWidth = self.titleLabel.intrinsicContentSize.width;
    CGFloat lableHeight = self.titleLabel.intrinsicContentSize.height;
//    self.intrinsicContentSize = (width = 60, height = 14)
//    self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
//    CGFloat totalHeight = (imageHeight + lableHeight + spacing);
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets titleEdgeInsets = UIEdgeInsetsZero;
    if (type == CAIButtonImagePlacementTop) {
        imageEdgeInsets = UIEdgeInsetsMake(- (lableHeight + spacing), 0, 0, - lableWidth);
        titleEdgeInsets = UIEdgeInsetsMake(0, - imageViewWidth, - (imageViewHeight+ spacing),0);
    }else if (type == CAIButtonImagePlacementLeft){
        imageEdgeInsets= UIEdgeInsetsMake(0, 0, 0,floorf(spacing/2));
        titleEdgeInsets= UIEdgeInsetsMake(0, floorf(spacing/2), 0, 0);
    }
    else if(type == CAIButtonImagePlacementRight){
        imageEdgeInsets = UIEdgeInsetsMake(0, lableWidth + spacing/2, 0, - lableWidth-spacing/2);
        titleEdgeInsets = UIEdgeInsetsMake(0, - imageViewWidth - spacing/2, 0, imageViewWidth + spacing/2);
    }
    self.imageEdgeInsets = imageEdgeInsets;
    self.titleEdgeInsets = titleEdgeInsets;
}


- (void)zx_setImagePositionWithTypeV2:(CAIButtonImagePlacement)type spacing:(CGFloat)spacing
{
    if (@available(iOS 15.0, *)) {
        UIButtonConfiguration *config = [UIButtonConfiguration plainButtonConfiguration];
        config.baseForegroundColor = self.currentTitleColor;
        config.baseBackgroundColor = self.backgroundColor?self.backgroundColor:[UIColor whiteColor];
        config.buttonSize = UIButtonConfigurationSizeMini;
        if (type == CAIButtonImagePlacementTop) {
            config.imagePlacement = NSDirectionalRectEdgeTop;
        }else if (type == CAIButtonImagePlacementLeft){
            config.imagePlacement = NSDirectionalRectEdgeLeading;
        }else if(type == CAIButtonImagePlacementRight){
            config.imagePlacement = NSDirectionalRectEdgeTrailing;
        }
        config.imagePadding = spacing;
        self.configuration = config;
    } else {
        ///self.frame:CGRect(0,0,0,0),self.imageView.frame:(5.5 0; 41 0)
    //    CGFloat imageWidth  = self.imageView.frame.size.width;
    //    CGFloat imageHeight = self.imageView.frame.size.height;
        CGFloat imageViewWidth  = self.imageView.intrinsicContentSize.width;
        CGFloat imageViewHeight = self.imageView.intrinsicContentSize.height;
        CGFloat lableWidth = self.titleLabel.intrinsicContentSize.width;
        CGFloat lableHeight = self.titleLabel.intrinsicContentSize.height;
    //    self.intrinsicContentSize = (width = 60, height = 14)
    //    self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
    //    CGFloat totalHeight = (imageHeight + lableHeight + spacing);
        UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
        UIEdgeInsets titleEdgeInsets = UIEdgeInsetsZero;
        if (type == CAIButtonImagePlacementTop) {
            imageEdgeInsets = UIEdgeInsetsMake(- (lableHeight + spacing), 0, 0, - lableWidth);
            titleEdgeInsets = UIEdgeInsetsMake(0, - imageViewWidth, - (imageViewHeight+ spacing),0);
        }else if (type == CAIButtonImagePlacementLeft){
            imageEdgeInsets= UIEdgeInsetsMake(0, 0, 0,floorf(spacing/2));
            titleEdgeInsets= UIEdgeInsetsMake(0, floorf(spacing/2), 0, 0);
        }
        else if(type == CAIButtonImagePlacementRight){
            imageEdgeInsets = UIEdgeInsetsMake(0, lableWidth + spacing/2, 0, - lableWidth-spacing/2);
            titleEdgeInsets = UIEdgeInsetsMake(0, - imageViewWidth - spacing/2, 0, imageViewWidth + spacing/2);
        }
        self.imageEdgeInsets = imageEdgeInsets;
        self.titleEdgeInsets = titleEdgeInsets;
    }
}



- (void)zx_setTowOfLinesStringWithLineSpace:(CGFloat)space
               firstLineWithAttributedTitle:(nullable NSAttributedString *)attributedTitle
              secondLineWithAttributedTitle:(nullable NSAttributedString *)attributedSubTitle
{
    self.titleLabel.numberOfLines = 0;

    NSMutableAttributedString *mAttributedString = [[NSMutableAttributedString alloc] init];
    if (attributedTitle) {
        [mAttributedString appendAttributedString:attributedTitle];
    }
    if (attributedTitle && attributedSubTitle) {
        NSAttributedString *lineAttri = [[NSAttributedString alloc] initWithString:@"\n"];
        [mAttributedString appendAttributedString:lineAttri];
    }
    if (attributedSubTitle) {
        [mAttributedString appendAttributedString:attributedSubTitle];
    }
        
    NSMutableParagraphStyle *paragraph = [[NSParagraphStyle defaultParagraphStyle]mutableCopy];
    paragraph.lineSpacing = space;
    paragraph.alignment = NSTextAlignmentCenter;
    
    NSDictionary *attributes = @{NSParagraphStyleAttributeName:paragraph};
    if (mAttributedString.length>0) {
        [mAttributedString addAttributes:attributes range:NSMakeRange(0, mAttributedString.length)];
    }
    [self setAttributedTitle:mAttributedString forState:UIControlStateNormal];
}


- (void)zx_setButtonImageViewScaleAspectFill
{
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.contentHorizontalAlignment= UIControlContentHorizontalAlignmentFill;
    self.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
}



- (void)zx_setEnabledTitleColor:(UIColor *)color1 disableTitleColor:(UIColor *)color2
{
    [self setTitleColor:color1 forState:UIControlStateNormal];
    [self setTitleColor:color2 forState:UIControlStateDisabled];
}


- (void)zx_setEnabledTitle:(NSString *)title1 disableTitle:(NSString *)title2
{
    [self setTitle:title1 forState:UIControlStateNormal];
    [self setTitle:title2 forState:UIControlStateDisabled];

}



- (void)zx_setSelectedWithImage:(UIImage *)image1 selectedImage:(UIImage *)image2
{
    [self setImage:image1 forState:UIControlStateNormal];
    [self setImage:image2 forState:UIControlStateSelected];
}

- (void)zx_setSelectedWithTitleColor:(UIColor *)color1 selectedTitleColor:(UIColor *)color2
{
    [self setTitleColor:color1 forState:UIControlStateNormal];
    [self setTitleColor:color2 forState:UIControlStateSelected];
}

- (void)zx_setSelectedWithAttributedTitle:(nullable NSAttributedString *)title selectedAttributedTitle:(nullable NSAttributedString *)title2
{
    [self setAttributedTitle:title forState:UIControlStateNormal];
    [self setAttributedTitle:title2 forState:UIControlStateSelected];
}

@end



